﻿namespace ACCOUNTING.LVY.Business
{
    using ACCOUNTING;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Data;
    using System.Runtime.InteropServices;
    using System.Windows.Forms;

    public class clsSoDuDauKyHangHoa
    {
        private string mDienGiai = "";
        private double mDonGia = 0.0;
        private string mHanSuDung = "";
        private double mHeSo = 0.0;
        private string mKhoa = "";
        private string mKhoaDonVi = "";
        private string mKhoaDonViTinh = "";
        private string mKhoaHangHoa = "";
        private string mKhoaKho = "";
        private string mKhoaNguoiDung = "";
        private string mKhoaQuayKe = "";
        private string mKhoaTKHangHoa = "";
        private string mLoaiTien = "";
        private string mNamThang = "";
        private string mNgayNhap = "";
        private string mSoLoHang = "";
        private double mSoLuong = 0.0;
        private string mSoSeri = "";
        private double mThanhTien = 0.0;
        private double mThanhTienNT = 0.0;
        private double mTyGia = 0.0;

        public bool DelData(string pKhoa)
        {
            bool flag=false;
            IDataParameter[] commandParameters = new IDataParameter[4];
            try
            {
                commandParameters[0] = DBHelper.createParameter("@Khoa", DbType.Int64, ParameterDirection.Input, pKhoa);
                commandParameters[1] = DBHelper.createParameter("@CheckAK", DbType.Double, ParameterDirection.Input, 1);
                commandParameters[2] = DBHelper.createParameter("@pAmKho", DbType.Double, ParameterDirection.Output, 0);
                commandParameters[3] = DBHelper.createParameter("@pError", DbType.Double, ParameterDirection.Output, 0);
                DBHelper.ExecuteNonQuery(modGeneral.ConnectionString, CommandType.StoredProcedure, "GL_sp_SoDuDauKyHangHoaDel", commandParameters);
                if (Microsoft.VisualBasic.CompilerServices.Operators.ConditionalCompareObjectNotEqual(commandParameters[3].Value, 0, false))
                {
                    if (Microsoft.VisualBasic.CompilerServices.Operators.ConditionalCompareObjectNotEqual(commandParameters[2].Value, 0, false))
                    {
                        LVYModule.ShowWarning(modGeneral.Tranlated("Nghiệp vụ kh\x00f4ng thể thực hiện được v\x00ec \x00e2m kho!", ""));
                    }
                    return false;
                }
                flag = true;
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                MessageBox.Show(exception.Message.ToString());
                flag = false;
                ProjectData.ClearProjectError();
                return flag;
                ProjectData.ClearProjectError();
            }
            return flag;
        }

        public DataTable GetList(string StrNamThang)
        {
            DataTable table;
            string commandText = "";
            try
            {
                commandText = " SELECT T.Khoa, K.Ten" + modGeneral.H_LANGUAGE + " As KhoHang, H.Ma as MaHang,  Rtrim(H.Ten" + modGeneral.H_LANGUAGE + ") as HangHoa, T.SoSeri, T.SoLoHang, T.NgayNhap,  T.HanSuDung,  DVT.Ten" + modGeneral.H_LANGUAGE + " as DonViTinh, T.HeSo, T.SoLuong, T.DonGia,  T.ThanhTienNT as ThanhTien, TK.SoTaiKhoan  FROM GL_SoDuDauKyHangHoa T left join DM_HangHoa H on T.KhoaHangHoa = H.Khoa  LEFT JOIN DM_DonViTinh DVT on T.KhoaDonViTinh = DVT.Khoa  LEFT JOIN DM_Kho K on T.KhoaKho = K.Khoa  LEFT JOIN DM_TaiKhoan TK on T.KhoaTKHangHoa = TK.Khoa  WHERE T.NamThang = '" + StrNamThang + "'";
                table = DBHelper.ExecuteDataset(modGeneral.ConnectionString, CommandType.Text, commandText, new IDataParameter[0]).Tables[0];
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                MessageBox.Show(exception.Message.ToString());
                table = null;
                ProjectData.ClearProjectError();
                return table;
                ProjectData.ClearProjectError();
            }
            return table;
        }

        public DataTable GetListHH(string pNamThang)
        {
            IDataParameter parameter = null;
            DataTable table;
            try
            {
                string commandText = " SELECT SDDKHH.Khoa, SDDKHH.NamThang, SDDKHH.KhoaKho,  K.TenVIET as KhoHang,SDDKHH.SoLuong,SDDKHH.DonGia,SDDKHH.ThanhTien, SDDKHH.KhoaHangHoa, HH.TenViet as TenHang, HH.Ma as MaHang, SDDKHH.SoSeri, SDDKHH.KhoaDonViTinh, DV.TenViet as DonViTinh  FROM GL_SoDuDauKyHangHoa SDDKHH  LEFT JOIN DM_HangHoa HH ON HH.KHOA = SDDKHH.KHOAHANGHOA  LEFT JOIN DM_Kho K ON K.KHOA = SDDKHH.KHOAKHO LEFT JOIN DM_DonViTinh DV ON DV.KHOA = SDDKHH.KHOADONVITINH WHERE NamThang = '" + pNamThang + "'";
                table = DBHelper.ExecuteDataset(modGeneral.ConnectionString, CommandType.Text, commandText, new IDataParameter[] { parameter }).Tables[0];
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                MessageBox.Show(exception.Message.ToString());
                table = null;
                ProjectData.ClearProjectError();
                return table;
                ProjectData.ClearProjectError();
            }
            return table;
        }

        public DataTable GetTonKhoDauKyHangHoa(string strNamThang, string strKhoaKho, int intLoai, string strKhoaNhom = "", string strQuocGia = "", string strKhoaHang = "")
        {
            DataTable table;
            string commandText = "";
            string str2 = "";
            str2 = " WHERE 1 = 1 ";
            if (strKhoaNhom.Trim() != "")
            {
                str2 = str2 + " AND HH.KhoaNhom = '" + strKhoaNhom.Trim() + "'";
            }
            if (strQuocGia.Trim() != "")
            {
                str2 = str2 + " AND HH.KhoaQuocGia = '" + strQuocGia.Trim() + "'";
            }
            if (strKhoaHang.Trim() != "")
            {
                str2 = str2 + " AND HH.KhoaHangSanXuat = '" + strKhoaHang.Trim() + "'";
            }
            if (intLoai == 1)
            {
                str2 = str2 + " AND DK.NamThang = '" + strNamThang + "'";
            }
            if (intLoai == 2)
            {
                str2 = str2 + " AND Rtrim(IsNull(DK.Khoa,'')) = ''";
            }
            try
            {
                commandText = " SELECT  IsNull(DK.Khoa,'') as Khoa , '' as KhoaQuay, '' as QuayKe, HH.Khoa as KhoaHangHoa,  HH.Ma as MaHang, HH.TenViet as TenHang, IsNull(DK.SoSeri,'') as SoSeri,  IsNull(DK.SoLoHang,'') as SoLoHang , IsNull(DK.NgayNhap,'') as NgayNhap, IsNull(DK.HanSuDung,'') as HanSuDung,  HH.KhoaDonViTinh, DVT.TenViet as DonViTinh, 1 as QuyCach, IsNull(DK.SoLuong,0) as SoLuong,  IsNull(DK.DonGia,0) as DonGia, IsNull(DK.ThanhTien,0) as ThanhTien, HH.KhoaTKHangHoa, TK.SoTaiKhoan  FROM DM_HangHoa HH left join GL_SoDuDauKyHangHoa DK  \ton HH.Khoa = DK.KhoaHangHoa and DK.NamThang = '" + strNamThang + "' and DK.KhoaKho = '" + strKhoaKho + "' \tleft join DM_TaiKhoan TK on HH.KhoaTKHangHoa = TK.Khoa \tleft join DM_DonViTinh DVT on HH.KhoaDonViTinh = DVT.Khoa " + str2 + " ORDER BY HH.Ma";
                table = DBHelper.ExecuteDataset(modGeneral.ConnectionString, CommandType.Text, commandText, new IDataParameter[0]).Tables[0];
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                MessageBox.Show(exception.Message.ToString());
                table = null;
                ProjectData.ClearProjectError();
                return table;
                ProjectData.ClearProjectError();
            }
            return table;
        }

        public bool Load(string pKhoa)
        {
            bool flag=false;
            IDataParameter[] commandParameters = new IDataParameter[2];
            try
            {
                string commandText = "SELECT * FROM GL_SoDuDauKyHangHoa WHERE Khoa = '" + pKhoa + "'";
                IDataReader dataReader = DBHelper.ExecuteReader(modGeneral.ConnectionString, CommandType.Text, commandText, commandParameters);
                if (dataReader.Read())
                {
                    this.mKhoa = Conversions.ToString(modGeneral.GetValue(dataReader, 0, modGeneral.typeOfColumn.GetString));
                    this.mNamThang = Conversions.ToString(modGeneral.GetValue(dataReader, 1, modGeneral.typeOfColumn.GetString));
                    this.mKhoaHangHoa = Conversions.ToString(modGeneral.GetValue(dataReader, 2, modGeneral.typeOfColumn.GetString));
                    this.mKhoaKho = Conversions.ToString(modGeneral.GetValue(dataReader, 3, modGeneral.typeOfColumn.GetString));
                    this.mKhoaDonViTinh = Conversions.ToString(modGeneral.GetValue(dataReader, 4, modGeneral.typeOfColumn.GetString));
                    this.mHeSo = Conversions.ToDouble(modGeneral.GetValue(dataReader, 5, modGeneral.typeOfColumn.GetDecimal));
                    this.mLoaiTien = Conversions.ToString(modGeneral.GetValue(dataReader, 6, modGeneral.typeOfColumn.GetString));
                    this.mTyGia = Conversions.ToDouble(modGeneral.GetValue(dataReader, 7, modGeneral.typeOfColumn.GetDecimal));
                    this.mSoLuong = Conversions.ToDouble(modGeneral.GetValue(dataReader, 8, modGeneral.typeOfColumn.GetDecimal));
                    this.mDonGia = Conversions.ToDouble(modGeneral.GetValue(dataReader, 9, modGeneral.typeOfColumn.GetDecimal));
                    this.mThanhTienNT = Conversions.ToDouble(modGeneral.GetValue(dataReader, 10, modGeneral.typeOfColumn.GetDecimal));
                    this.mThanhTien = Conversions.ToDouble(modGeneral.GetValue(dataReader, 11, modGeneral.typeOfColumn.GetDecimal));
                    this.mKhoaTKHangHoa = Conversions.ToString(modGeneral.GetValue(dataReader, 12, modGeneral.typeOfColumn.GetString));
                    this.mDienGiai = Conversions.ToString(modGeneral.GetValue(dataReader, 13, modGeneral.typeOfColumn.GetString));
                    this.mKhoaNguoiDung = Conversions.ToString(modGeneral.GetValue(dataReader, 14, modGeneral.typeOfColumn.GetString));
                    this.mKhoaDonVi = Conversions.ToString(modGeneral.GetValue(dataReader, 15, modGeneral.typeOfColumn.GetString));
                    this.mSoSeri = Conversions.ToString(modGeneral.GetValue(dataReader, 0x10, modGeneral.typeOfColumn.GetString));
                    this.mNgayNhap = Conversions.ToString(modGeneral.GetValue(dataReader, 0x11, modGeneral.typeOfColumn.GetString));
                    this.mHanSuDung = Conversions.ToString(modGeneral.GetValue(dataReader, 0x12, modGeneral.typeOfColumn.GetString));
                    this.mKhoaQuayKe = Conversions.ToString(modGeneral.GetValue(dataReader, 0x13, modGeneral.typeOfColumn.GetString));
                    this.mSoLoHang = Conversions.ToString(modGeneral.GetValue(dataReader, 20, modGeneral.typeOfColumn.GetString));
                }
                dataReader.Close();
                flag = true;
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                MessageBox.Show(exception.Message.ToString());
                flag = false;
                ProjectData.ClearProjectError();
                return flag;
                ProjectData.ClearProjectError();
            }
            return flag;
        }

        public bool Save()
        {
            bool flag=false;
            IDataParameter[] commandParameters = new IDataParameter[0x17];
            try
            {
                commandParameters[0] = DBHelper.createParameter("@Khoa", DbType.Int64, ParameterDirection.Input, this.mKhoa);
                commandParameters[1] = DBHelper.createParameter("@NamThang", DbType.Int64, ParameterDirection.Input, this.mNamThang);
                commandParameters[2] = DBHelper.createParameter("@KhoaHangHoa", DbType.Int64, ParameterDirection.Input, this.mKhoaHangHoa);
                commandParameters[3] = DBHelper.createParameter("@KhoaKho", DbType.Int64, ParameterDirection.Input, this.mKhoaKho);
                commandParameters[4] = DBHelper.createParameter("@KhoaDonViTinh", DbType.Int64, ParameterDirection.Input, this.mKhoaDonViTinh);
                commandParameters[5] = DBHelper.createParameter("@HeSo", DbType.Date, ParameterDirection.Input, this.mHeSo);
                commandParameters[6] = DBHelper.createParameter("@LoaiTien", DbType.Int64, ParameterDirection.Input, this.mLoaiTien);
                commandParameters[7] = DBHelper.createParameter("@TyGia", DbType.Date, ParameterDirection.Input, this.mTyGia);
                commandParameters[8] = DBHelper.createParameter("@SoLuong", DbType.Date, ParameterDirection.Input, this.mSoLuong);
                commandParameters[9] = DBHelper.createParameter("@DonGia", DbType.Date, ParameterDirection.Input, this.mDonGia);
                commandParameters[10] = DBHelper.createParameter("@ThanhTienNT", DbType.Date, ParameterDirection.Input, this.mThanhTienNT);
                commandParameters[11] = DBHelper.createParameter("@ThanhTien", DbType.Date, ParameterDirection.Input, this.mThanhTien);
                commandParameters[12] = DBHelper.createParameter("@KhoaTKHangHoa", DbType.Int64, ParameterDirection.Input, this.mKhoaTKHangHoa);
                commandParameters[13] = DBHelper.createParameter("@DienGiai", DbType.Int64, ParameterDirection.Input, this.mDienGiai);
                commandParameters[14] = DBHelper.createParameter("@KhoaNguoiDung", DbType.Int64, ParameterDirection.Input, this.mKhoaNguoiDung);
                commandParameters[15] = DBHelper.createParameter("@KhoaDonVi", DbType.Int64, ParameterDirection.Input, this.mKhoaDonVi);
                commandParameters[0x10] = DBHelper.createParameter("@SoSeri", DbType.Int64, ParameterDirection.Input, this.mSoSeri);
                commandParameters[0x11] = DBHelper.createParameter("@NgayNhap", DbType.Int64, ParameterDirection.Input, this.mNgayNhap);
                commandParameters[0x12] = DBHelper.createParameter("@HanSuDung", DbType.Int64, ParameterDirection.Input, this.mHanSuDung);
                commandParameters[0x13] = DBHelper.createParameter("@KhoaQuayKe", DbType.Int64, ParameterDirection.Input, this.mKhoaQuayKe);
                commandParameters[20] = DBHelper.createParameter("@SoLoHang", DbType.Int64, ParameterDirection.Input, this.mSoLoHang);
                commandParameters[0x15] = DBHelper.createParameter("@pAmKho", DbType.Double, ParameterDirection.Output, 0);
                commandParameters[0x16] = DBHelper.createParameter("@pError", DbType.Double, ParameterDirection.Output, 0);
                DBHelper.ExecuteNonQuery(modGeneral.ConnectionString, CommandType.StoredProcedure, "GL_sp_SoDuDauKyHangHoa", commandParameters);
                if (Microsoft.VisualBasic.CompilerServices.Operators.ConditionalCompareObjectNotEqual(commandParameters[0x16].Value, 0, false))
                {
                    if (Microsoft.VisualBasic.CompilerServices.Operators.ConditionalCompareObjectNotEqual(commandParameters[0x15].Value, 0, false))
                    {
                        LVYModule.ShowWarning(modGeneral.Tranlated("Nghiệp vụ kh\x00f4ng thể thực hiện được v\x00ec \x00e2m kho!", ""));
                    }
                    return false;
                }
                flag = true;
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                MessageBox.Show(exception.Message.ToString());
                flag = false;
                ProjectData.ClearProjectError();
                return flag;
                ProjectData.ClearProjectError();
            }
            return flag;
        }

        public string SearchByCode(string strCode, string strConditions = "")
        {
            string str4;
            string commandText = "";
            string str3 = "";
            string str = "";
            try
            {
                if (strCode.Trim() != "")
                {
                    str3 = " AND RTRIM(Ma) = '" + strCode.Trim() + "'";
                }
                if (strConditions.Trim() != "")
                {
                    str3 = str3 + " AND " + strConditions;
                }
                commandText = "SELECT Khoa, Ma, Ten" + modGeneral.H_LANGUAGE + " as Ten  FROM GL_SoDuDauKyHangHoa WHERE Active = 1 " + str3;
                IDataReader dataReader = DBHelper.ExecuteReader(modGeneral.ConnectionString, CommandType.Text, commandText, new IDataParameter[0]);
                if (dataReader.Read())
                {
                    str = modGeneral.GetValue(dataReader, 0, modGeneral.typeOfColumn.GetString).ToString() + "|" + modGeneral.GetValue(dataReader, 1, modGeneral.typeOfColumn.GetString).ToString() + "|" + modGeneral.GetValue(dataReader, 2, modGeneral.typeOfColumn.GetString).ToString();
                }
                dataReader.Close();
                str4 = str;
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                MessageBox.Show(exception.Message.ToString());
                str4 = "";
                ProjectData.ClearProjectError();
                return str4;
                ProjectData.ClearProjectError();
            }
            return str4;
        }

        public string DienGiai
        {
            get
            {
                return this.mDienGiai;
            }
            set
            {
                this.mDienGiai = value;
            }
        }

        public double DonGia
        {
            get
            {
                return this.mDonGia;
            }
            set
            {
                this.mDonGia = value;
            }
        }

        public string HanSuDung
        {
            get
            {
                return this.mHanSuDung;
            }
            set
            {
                this.mHanSuDung = value;
            }
        }

        public double HeSo
        {
            get
            {
                return this.mHeSo;
            }
            set
            {
                this.mHeSo = value;
            }
        }

        public string Khoa
        {
            get
            {
                return this.mKhoa;
            }
            set
            {
                this.mKhoa = value;
            }
        }

        public string KhoaDonVi
        {
            get
            {
                return this.mKhoaDonVi;
            }
            set
            {
                this.mKhoaDonVi = value;
            }
        }

        public string KhoaDonViTinh
        {
            get
            {
                return this.mKhoaDonViTinh;
            }
            set
            {
                this.mKhoaDonViTinh = value;
            }
        }

        public string KhoaHangHoa
        {
            get
            {
                return this.mKhoaHangHoa;
            }
            set
            {
                this.mKhoaHangHoa = value;
            }
        }

        public string KhoaKho
        {
            get
            {
                return this.mKhoaKho;
            }
            set
            {
                this.mKhoaKho = value;
            }
        }

        public string KhoaNguoiDung
        {
            get
            {
                return this.mKhoaNguoiDung;
            }
            set
            {
                this.mKhoaNguoiDung = value;
            }
        }

        public string KhoaQuayKe
        {
            get
            {
                return this.mKhoaQuayKe;
            }
            set
            {
                this.mKhoaQuayKe = value;
            }
        }

        public string KhoaTKHangHoa
        {
            get
            {
                return this.mKhoaTKHangHoa;
            }
            set
            {
                this.mKhoaTKHangHoa = value;
            }
        }

        public string LoaiTien
        {
            get
            {
                return this.mLoaiTien;
            }
            set
            {
                this.mLoaiTien = value;
            }
        }

        public string NamThang
        {
            get
            {
                return this.mNamThang;
            }
            set
            {
                this.mNamThang = value;
            }
        }

        public string NgayNhap
        {
            get
            {
                return this.mNgayNhap;
            }
            set
            {
                this.mNgayNhap = value;
            }
        }

        public string SoLoHang
        {
            get
            {
                return this.mSoLoHang;
            }
            set
            {
                this.mSoLoHang = value;
            }
        }

        public double SoLuong
        {
            get
            {
                return this.mSoLuong;
            }
            set
            {
                this.mSoLuong = value;
            }
        }

        public string SoSeri
        {
            get
            {
                return this.mSoSeri;
            }
            set
            {
                this.mSoSeri = value;
            }
        }

        public double ThanhTien
        {
            get
            {
                return this.mThanhTien;
            }
            set
            {
                this.mThanhTien = value;
            }
        }

        public double ThanhTienNT
        {
            get
            {
                return this.mThanhTienNT;
            }
            set
            {
                this.mThanhTienNT = value;
            }
        }

        public double TyGia
        {
            get
            {
                return this.mTyGia;
            }
            set
            {
                this.mTyGia = value;
            }
        }
    }
}

